const apiMethods = {
	methods: {
		apiGet(url, data) {
			let that = this;
			return new Promise((resolve, reject) => {
				axios.get(url, data).then((response) => {
					if(response.data.code == 200) {
						resolve(response.data)	
					}else if(response.data.code == 1009) {
						bus.$message({
							message: response.data.err,
							type: 'warning'
						})
						that.goLoginPage()
					}else {
						bus.$message({
							message: response.data.err,
							type: 'error'
						})
					}
				}, (response) => {
					_g.closeGlobalLoading()
					bus.$message({
						message: '捕获异常',
						type: 'warning'
					})
					that.goLoginPage()
					reject(response)
				})
			})
		},
		apiPost(url, data) {
			let that = this;
			return new Promise((resolve, reject) => {
				axios.post(url, data).then((response) => {
					if(response.data.code == 200) {
						resolve(response.data)	
					}else if(response.data.code == 1009) {
						bus.$message({
							message: response.data.err,
							type: 'warning'
						})
						that.goLoginPage()
					}else {
						bus.$message({
							message: response.data.err,
							type: 'error'
						})
//						that.goLoginPage()
					}
				}).catch((response) => {
					_g.closeGlobalLoading()
					bus.$message({
						message: '捕获异常',
						type: 'warning'
					})
					that.goLoginPage()
					resolve(response)
				})
			})
		},
		apiDelete(url, id) {
			return new Promise((resolve, reject) => {
				//    	url = domain+url;
				axios.delete(url + id).then((response) => {
					resolve(response.data)
				}, (response) => {
					reject(response)
					//        _g.closeGlobalLoading()
					//        bus.$message({
					//          message: '请求超时，请检查网络',
					//          type: 'warning'
					//        })
				})
			})
		},
		apiPut(url, id, obj) {
			return new Promise((resolve, reject) => {
				//    	url = domain+url;
				axios.put(url + id, obj).then((response) => {
					resolve(response.data)
				}, (response) => {
					//        _g.closeGlobalLoading()
					//        bus.$message({
					//          message: '请求超时，请检查网络',
					//          type: 'warning'
					//        })
					reject(response)
				})
			})
		},
		goLoginPage() {
			this.$router.push({
				path: '/login'
			})
		}
	},
	computed: {
		showLoading() {
			return store.state.globalLoading
		}
	}
}

export default apiMethods